function createTableHead(head){//表头
    let tableHead= '';
    for(let i = 0;i<head.length;i++){
      tableHead += `<th>${head[i]}</th>`
    }
    tou.innerHTML = tableHead;
}
function createTbody(area,goods){//渲染表格数据
    var data = getData(area,goods);
    let bBody = '';
    for(let i =0;i<data.length;i++){
      let bTr = `<td>${data[i].region}</td><td>${data[i].product}</td>`;
      
      if(area.length > goods.length && goods.length == 1){
        bTr = `<td>${data[i].product}</td><td>${data[i].region}</td>`;
       
      } 
      for(let k =0;k<data[i].sale.length;k++){
        bTr += `<td>${data[i].sale[k]}</td>`
      }
      bTr = `<tr>${bTr}</tr>`;
      bBody += bTr
    }
    shen.innerHTML = bBody;
    if(goods.length  == 1 && area.length > 1){
        createTableHead(tHead);
        mergeCell('table', 0, 0, 0);
    }else if(goods.length  > 1 && area.length == 1){
        createTableHead(tHead1);
        mergeCell('table', 0, 0, 0);
    }else if(goods.length  > 1 && area.length >1){
        createTableHead(tHead1);
        mergeCell('table', 0, 0, 0);
        mergeCell('table', 0, 0, 1);
    }
   
}
function mergeCell(table1, startRow, endRow, col) {//合并单元格
    var tb = document.getElementById(table1);
    if(!tb || !tb.rows || tb.rows.length <= 0) {
        return;
    }
    if(col >= tb.rows[0].cells.length || (startRow >= endRow && endRow != 0)) {
        return;
    }
    if(endRow == 0) {
        endRow = tb.rows.length - 1;
    }
    for(var i = startRow; i < endRow; i++) {
        // console.log(tb.rows[i + 1].cells[3])
        if(tb.rows[startRow].cells[col].innerHTML == tb.rows[i + 1].cells[col].innerHTML) { //如果相等就合并单元格,合并之后跳过下一行
            tb.rows[i + 1].removeChild(tb.rows[i + 1].cells[col]);
            tb.rows[startRow].cells[col].rowSpan = (tb.rows[startRow].cells[col].rowSpan) + 1;
        } else {
            mergeCell(table1, i + 1, endRow, col);
            break;
        }
    }
}
function getData(area,goods){//获取tbody的数据
    let data = [];
    for(let i = 0;i<sourceData.length;i++){
        if(area.indexOf(sourceData[i].region) !== -1 && goods.indexOf(sourceData[i].product) !== -1){
            data.push(sourceData[i]);
        }
    }
    return data;
}